home *** CD-ROM | disk | FTP | other *** search
- {
-
- TechTutor1
- Bullets using linked-list technique
-
- Coding by P.Bestebroer
- FreeWare
-
- This source will not work on it's own, it just shows how to
- do bullet-code into you'r game...
-
- Contacting:
-
- HTTP://people.zeelandnet.nl/rpb/
- EMAIL:just4fun@zeelandnet.nl
-
- }
- PROGRAM Tech01;
-
- USES CRT;
- {─────────────────────────────────────────────────────────────────────────────}
- {
- First create the variables
-
- }
- TYPE PBullet = ^TBullet;
- TBullet = record
- xpos,ypos : integer;
- xspeed,yspeed : integer;
- f_Frame : byte; { image frame }
- ai : byte; { bullet AI identifier }
- subai : byte; { person who shot it }
- prev,next : Pbullet;{ link to next/previous bullet }
- END;
-
- VAR FirstBullet : PBullet; { first bullet in list }
- LastBullet : PBullet; { last bullet in list }
- {─────────────────────────────────────────────────────────────────────────────}
- {
- The addbullet procedure will add a bullet to the linked-list.
-
- Expects: Xposition, Yposition, XSpeed, YSpeed
- Image-Frame number, BULLET-AI, shot-by-ai
- Returns: TRUE if bullet was added to the list.
- }
- FUNCTION AddBullet( Xpos2,Ypos2,xspeed2,yspeed2 : integer;
- F_Frame2,ai2,subai2 : byte):boolean;
- VAR TempBull : PBullet; { this is used to create the new-bullet }
-
- BEGIN
- AddBullet:=false; { No bullet is added upto this point }
-
- new(TempBull); { Get memory for new bullet }
-
- if TempBull=nil then exit; { if TEMP=NIL, we don't have enough memory }
-
- if FirstBullet=NIL then begin { is the list empty? }
- LastBullet:=TempBull; { yes, so the NEW-BULLET is the first+last }
- FirstBullet:=TempBull;
- with FirstBullet^ do begin
- next:=NIL; { next/previous are nothing, because there }
- prev:=Nil; { is only one bullet in the list }
- end;
- end else begin { More bullets in the list! }
- LastBullet^.next:=tempbull;{ Lastbullet points to new-bullet }
- TempBull^.prev:=LastBullet;{ lastbullet is not lastbullet anymore }
- LastBullet:=TempBull; { Lastbullet=NEWBULLET }
- Tempbull^.next:=NIL; { no bullets after the new-bullet }
- end;
-
- With TempBull^ do begin { Set the new values }
- xpos:=xpos2+xspeed2;
- ypos:=ypos2+yspeed2;
- xspeed:=xspeed2;
- yspeed:=yspeed2;
-
- ai :=ai2;
- subai :=subai2;
- f_Frame :=f_Frame2;
- end;
-
- AddBullet:=True; { the bullet was added }
- END;
- {─────────────────────────────────────────────────────────────────────────────}
- {
-
- Erase a BULLET object from the linked-list
-
- Expects: pointer to BULLET object that needs to be ERASED
- Returns: True if succesfull
-
- }
- FUNCTION EraseBullet(Temp:PBullet):boolean;
- VAR next_bull,
- prev_bull : PBullet;
- BEGIN
- EraseBullet:=false; { bullet not yet erased }
- if Temp=NIL then exit; { if bullet=NIL then just exit }
-
- prev_BULL:=Temp^.prev; { Correct the bullet pointers }
- next_BULL:=Temp^.next;
-
- if prev_BULL=NIL then begin { if there is no previous bullet, then }
- FirstBullet:=next_Bull; { we'r working with the firstbullet }
- end else
- Prev_bull^.next:=next_bull;
-
- if next_bull=NIL then begin { if there is no next bullet , then }
- LastBullet:=prev_Bull; { we'r working with the lastbullet }
- end else
- Next_Bull^.prev:=prev_Bull;
-
- dispose(Temp); { dispose the bullet, freeing up memory }
- END;
-
- {─────────────────────────────────────────────────────────────────────────────}
- {
- The DOBULLETS procedure will process the linked-list
-
- Expects: Nothing
- Returns: Nothing
- }
- PROCEDURE DoBullets;
- VAR temp : PBullet; { temporary bullet }
- next_Bull : PBullet; { next bullet in list }
- done : boolean; { bullet done? }
- BEGIN
- Temp:=FirstBullet; { start with first bullet }
-
- WHILE Temp<>Nil do begin { while not pointing to NIL, do bullets }
- with Temp^ do begin
- done:=false; { bullet not yet done }
- next_Bull:=temp^.next; { pointer to Next-bullet in list }
-
- Case AI of { check on bullet AI }
- 1 : BEGIN { normal bullet }
- inc(xpos,xspeed); { increase X position }
- inc(ypos,yspeed); { increase Y position }
- {
- ....
- Check boundaries of screen
- and check for wall-background blocks
- ....
- }
- { Bullet hits something, or is off-screen }
- if not EraseBullet(temp) then begin
- { ERROR! bullet could not be disposed }
- halt(1);
- end;
- Done:=True; { Bullet is done }
- END;
- END;{CASE ai }
-
- { if bullet was not "erased" continue the procedure }
- If Not done then begin { see if we hit the player }
- {
- ....
- See if bullet is not shot by player
- ....
- }
- {
- ....
- Check with player coordinates
- ....
- }
- { IF HIT PLAYER THEN BEGIN }
- { Bullet hits player, and is erased }
- if not EraseBullet(temp) then begin
- { ERROR! bullet could not be disposed }
- halt(1);
- end;
- done:=true;
- end;
- { if bullet was not "erased" continue the procedure }
- if not done then
- {
- ....
- Draw the image on the screen!
- ....
- }
- end;
- temp:=next_Bull; { get pointer to next bullet in the list }
- end;
- END;
- {─────────────────────────────────────────────────────────────────────────────}
- BEGIN
- FirstBullet:=Nil; { No first bullet in list }
- LastBullet:=Nil; { and no last bullet in list... }
-
- while port[$60]<>156 do ;
- textcolor(7); textbackground(0); clrscr;
- writeln('TechTutor #1');
- writeln('written by P.Bestebroer, Just4Fun Productions');
- writeln('');
- writeln('This topic is for adding bullets to your games, since I don''t');
- writeln('feel like writing a complete example of an implementation in a game');
- writeln('this example wont work on it self.');
- writeln;
- writeln('The only major changes to get this working are in the DOBULLETS procedure');
- writeln('You should use a great VGA unit (SuperFX engine for example ;) and ');
- writeln('implement the things like drawing the bullets etc..');
- writeln;
- writeln('Watch out for the other techtutors...');
- writeln;
- writeln('Press a key');
-
- writeln;
- writeln;
- writeln;
- writeln('----------------------------------');
- writeln('Contacting: just4fun@zeelandnet.nl');
- writeln('http://people.zeelandnet.nl/rpb ');
- writeln('----------------------------------');
- repeat until port[$60]<>156;
- END.
-